<?php

/**
 * 餐厅管理
 * 
 * @copyright xinwei
 * @since 2012-04-07
 */
$_GET['db_debug'] = true;
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	//关键词
	cgi::get($g_cgival["keyword"], "keyword", "");
	$g_cgival["keyword"] = string::un_script_code($g_cgival["keyword"]);
	$g_cgival["keyword"] = string::un_html($g_cgival["keyword"]);
	if($g_cgival["keyword"] == "按餐厅名称/别名/地址检索")
	{
		$g_cgival["keyword"] = "";
	}
	
	//菜系
	cgi::get($g_cgival["RstrtFlavorByAddr"], "RstrtFlavorByAddr", 0);
	$g_cgival["RstrtFlavorByAddr"] = intval($g_cgival["RstrtFlavorByAddr"]);

	//风味
	cgi::get($g_cgival["RstrtFlavorByType"], "RstrtFlavorByType", 0);
	$g_cgival["RstrtFlavorByType"] = intval($g_cgival["RstrtFlavorByType"]);
	
	//管理员ID
	cgi::get($g_cgival["admin_id"], "admin_id", 0);
	$g_cgival["admin_id"] = intval($g_cgival["admin_id"]);
	
	//审核状态
	cgi::get($g_cgival["verify"], "verify", '0');
	$g_cgival["verify"] = string::un_script_code($g_cgival["verify"]);
	$g_cgival["verify"] = string::un_html($g_cgival["verify"]);
	
	//页码
	cgi::get($g_cgival["p"], "p", 1);
	$g_cgival["p"] = intval($g_cgival["p"]);
	
	//长度
	cgi::get($g_cgival["pc"], "pc", 16);
	$g_cgival["pc"] = intval($g_cgival["pc"]);
	
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show,$config;
	
	$db = new db_web_rstrt();
	$db_cname = new db_web_rstrt_cname();
	$db_tag = new db_food_tag();
	$db_admin = new db_admin_user();
	$user_db = new db_food_user();
	
	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();
	
	// 获取当前site下的管理员列表
	$g_show["admin_list"] = $db_admin->get_user_list_by_site_id($site_id,1);
	
	
	//菜系
	$styleArr = $db_tag->admin_shangjia_search($site_id, '菜系', '', 0, -1, 1, 10000);
	foreach($styleArr as $i => $val)
	{
		$g_show["style"][$val["id"]] = $val["tag_name"];
	}

	//风味
	$flavorArr = $db_tag->admin_shangjia_search($site_id, '食尚', '', 1, -1, 1, 10000);
	foreach($flavorArr as $i => $val)
	{
		$g_show["flavor"][$val["id"]] = $val["tag_name"];
	}	

	// 按条件获取专辑列表
	$data['rstrt_list'] = $db->admin_get_rstrt_list_by
	(
		$site_id,
		$g_cgival["keyword"],
		$g_cgival["RstrtFlavorByAddr"],//风味
		$g_cgival["RstrtFlavorByType"],//菜系
		$g_cgival["admin_id"],
		$g_cgival["verify"],//审核状态
		$g_cgival["p"],
		$g_cgival["pc"]
	);
	
	$user_global_config =  user_global_config::get_instance();
	// 显示数据处理
	if(count($data['rstrt_list'])>0)
	{
		foreach($data['rstrt_list'] as $i => $val)
		{
			
			//前台URL拼装
			
			$domain = $user_global_config->get_domain_by_site_id($site_id);			// 当前用户的站点ID
			$data['rstrt_list'][$i]['domain'] = $domain;
			
			
			
			// 获取分店信息
//			echo $val['RstrtChain']."-".$val['RstrtPID']."<BR>";
			if($val['RstrtChain'] == '1' && $val['RstrtPID'] == 0)
			{
				
				$data['rstrt_list'][$i]['RstrtChainInfo']['is_m'] = 2; //标记为总店
				$data['rstrt_list'][$i]['RstrtChainInfo']['chain_count'] = $db->admin_get_rstrt_chain_count($val['RstrtID']);
				$data['rstrt_list'][$i]['RstrtChainInfo']['chain_info'] = $db->get_list_shops_by_rstrt_id($val['RstrtID']);
			}
			elseif($val['RstrtChain'] == '1' && $val['RstrtPID'] != 0)
			{
				$data['rstrt_list'][$i]['RstrtChainInfo']['is_m'] = 1; //标记为分店
				$m_info = $db->admin_get_rstrt_by_ids(array($val['RstrtID']));
				$data['rstrt_list'][$i]['RstrtChainInfo']['master'] = $m_info[0];
			}
			else
			{
				$data['rstrt_info']['RstrtChainInfo']['is_m'] = 0;
			}
			//ID组合
			$rstrt_id_array[] = $val['RstrtID'];
		}
		// 获取别名
		$cnameArr = $db_cname->admin_get_cname_list_by_id_array($rstrt_id_array,1,1000);
		if(!empty($cnameArr))
		{
			$cnameArr2 = array();
			foreach($cnameArr as $i => $val)
			{
				$cnameArr2[$val["res_id"]][] = $val['cname'];
			}
			foreach($cnameArr2 as $ii => $vv)
			{
				$cnameArr2[] = $vv['cname'];
				$data['cname_list'][$ii] = implode("," , $vv);
			}
			
		}
		
		foreach($data['rstrt_list'] as $i => $val)
		{
			if(isset($data['cname_list'][$val['RstrtID']]))
			{
				$data['rstrt_list'][$i]["cname"] = $data['cname_list'][$val['RstrtID']];
			}
			else
			{
				$data['rstrt_list'][$i]["cname"] = "-";
			}
		}
		
	}
	


	
	$g_show["data"] = $data;
	
	// Pager 
	$g_show["count"] = $db->admin_get_rstrt_count
	(
		$site_id,
		$g_cgival["keyword"],
		$g_cgival["RstrtFlavorByAddr"],//风味
		$g_cgival["RstrtFlavorByType"],//菜系
		$g_cgival["admin_id"],
		$g_cgival["verify"]//审核状态
	);
	
	$max_page = page_bar::get_page_max($g_show["count"], $g_cgival["pc"], $g_cgival["p"]);
	$page_bar_style = array(
			"curt"		=> $g_cgival["p"],
			"max"		=> $max_page,
			"key"		=> "p",
	);
	
	$g_show["page_bar"] = "<b>{$g_show["count"]}</b> 条记录&nbsp;&nbsp;第{$g_cgival["p"]}页&nbsp;共{$max_page}页&nbsp;" . page_bar::bar3($page_bar_style);
	
	
	// 当前参数输出到页面	
	$g_show['args'] = $g_cgival;
}

$g_show["error"] = 0;
$g_show["errmsg"] = '';
try
{
	admin_check_login();
// 	admin_check_user_priv("foodv2/album_list.php");
	
	check_cgi_pro();
	get_data();
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_cgival, $g_pro, $g_show;
	if(isset($_GET['debug']))
		print_r($g_show);
	$smarty = p_get_smarty ();
	$smarty->assign ( 'g_show', $g_show );
	$smarty->display ( "foodv2/rstrt/rstrt_list.html" );
}

?>